import { PetMingBaseItem } from "./PetMingBaseItem";
import { EPlayerItemPosType } from "../../../../../Message/Public/EPlayerItemPosType";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { Cache } from "../../../cache/Cache";
import { ToolTipsManager } from "../../../../../com/mui/manager/ToolTipsManager";
import { Language } from "../../../../../extend/language/Language";
import { DataEvent } from "../../../events/DataEvent";
import { Global } from "../../../../../com/gengine/global/Global";
import { CursorManager } from "../../../manager/CursorManager";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { PetMingItemData } from "../info/PetMingItemData";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
//class PetMingBagBaseItem
    
    export  class PetMingBagBaseItem /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.baseItem = new PetMingBaseItem();
            this.baseItem.posType = EPlayerItemPosType._EPlayerItemPosTypePetLifeStypePut;
            this.baseItem.isShowLevel = true;
            this.baseItem.isWordWrap = true;
            this.baseItem.doubleClickEnabled = true;
            this.baseItem._levelTxt.y = 50;
            this.addChild(this.baseItem);
            this.addEventListener(flash.events.MouseEvent.CLICK, this.clickHander);
            Dispatcher.addEventListener(EventName.PetMingLockClick, this.setHintEnabed);
            return;
        }

        /* internal  */setHintEnabed(arg1: Object /* flash.events.Event */): void
        {
            var loc1=Cache.instance.petMing.lockIsSelect;
            if (this._lockedIconBox && loc1 == false) 
            {
                ToolTipsManager.register(this._lockedIconBox, null, Language.getString(54932));
            }
            else 
            {
                ToolTipsManager.unregister(this._lockedIconBox);
            }
            return;
        }

        /* internal  */clickHander(arg1: Object /* flash.events.Event */): void
        {
            if (!(this.baseItem.itemData == null) && !Cache.instance.petMing.isOneEat && Cache.instance.petMing.lockIsSelect) 
            {
                this.sendToServerLock(!this._hasLockFlag);
            }
            return;
        }

        /* internal  */sendToServerLock(arg1: Boolean): void
        {
            if (this.baseItem.itemData != null) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetMingLock, {"lockFlag": arg1, "items":[this.baseItem.itemData], "callBackFun": this.serverBackHander}));
            }
            return;
        }

        /* internal  */serverBackHander(arg1: Boolean, arg2: Array<any>=null): void
        {
            var loc3=null;
            this.setHasLockFlag(arg1);
            var loc1=Global.stage.getObjectsUnderPoint(new Object /* flash.geom.Point */(Global.stage.mouseX, Global.stage.mouseY));
            var loc2=0;
            while (loc2 < loc1.length) 
            {
                loc3 = loc1[loc2];
                if (this.contains(loc3)) 
                {
                    if (this.itemData.isLock) 
                    {
                        CursorManager.showCursor(CursorManager.PetMingUnLock);
                    }
                    else 
                    {
                        CursorManager.showCursor(CursorManager.PetMingLock);
                    }
                    return;
                }
                ++loc2;
            }
            return;
        }

        /* internal  */showToUnlockHander(arg1: Object /* flash.events.Event */): void
        {
            if (Cache.instance.petMing.lockIsSelect || Cache.instance.petMing.isOneEat) 
            {
                return;
            }
            if (this._hasLockFlag) 
            {
                arg1.stopPropagation();
                arg1.stopImmediatePropagation();
                arg1.preventDefault();
                unlockMenu.visible = true;
                this.parent.parent.addChild(unlockMenu);
                unlockMenu.x = this.parent.x + 40;
                if (unlockMenu.x > 300) 
                {
                    unlockMenu.x = unlockMenu.x - 105;
                }
                unlockMenu.y = this.parent.y + 25;
                Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.toHideUnlockMenuHander);
                this.baseItem.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.toHideUnlockMenuHander);
            }
            return;
        }

        /* internal  */toHideUnlockMenuHander(arg1: Object /* flash.events.Event */): void
        {
            var loc1=null;
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.toHideUnlockMenuHander);
            this.baseItem.removeEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.toHideUnlockMenuHander);
            unlockMenu.visible = false;
            if (arg1 != null) 
            {
                loc1 = arg1.target as Object /* flash.display.DisplayObject */;
                if (unlockMenu.contains(loc1)) 
                {
                    this.sendToServerLock(false);
                }
            }
            return;
        }

        public setHasLockFlag(arg1: Boolean): void
        {
            var loc1=null;
            this._hasLockFlag = arg1;
            if (arg1) 
            {
                if (!this._lockedIconBox) 
                {
                    this._lockedIconBox = new Object /* flash.display.Sprite */();
                    loc1 = GlobalClass.getBitmap("RecastAttribute_Lock");
                    var loc2;
                    loc1.y = loc2 = 4;
                    loc1.x = loc2;
                    this._lockedIconBox.addChild(loc1);
                    addChild(this._lockedIconBox);
                    this._lockedIconBox.doubleClickEnabled = true;
                    this._lockedIconBox.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.doubleClickHander);
                    this.setHintEnabed(null);
                }
                this._lockedIconBox.visible = true;
                this.baseItem.addEventListener(flash.events.MouseEvent.CLICK, this.showToUnlockHander);
            }
            else 
            {
                if (this._lockedIconBox) 
                {
                    this._lockedIconBox.visible = false;
                }
                this.baseItem.removeEventListener(flash.events.MouseEvent.CLICK, this.showToUnlockHander);
            }
            this.baseItem.isDragAble = !this._hasLockFlag;
            return;
        }

        /* internal  */doubleClickHander(arg1: Object /* flash.events.Event */): void
        {
            arg1.stopImmediatePropagation();
            arg1.stopPropagation();
            arg1.preventDefault();
            this.sendToServerLock(false);
            return;
        }

        public set itemData(arg1: PetMingItemData)
        {
            this.baseItem.itemData = arg1;
            if (arg1) 
            {
                if (arg1.name.length > 4) 
                {
                    this.baseItem._nameTxt.y = 25;
                }
                else 
                {
                    this.baseItem._nameTxt.y = 30;
                }
                this.setHasLockFlag(arg1.isLock);
            }
            else 
            {
                this.setHasLockFlag(false);
            }
            return;
        }

        public PetMingBagBaseItem.get itemData(): PetMingItemData
        {
            return this.baseItem.itemData as PetMingItemData;
        }

        /* internal  */static get unlockMenu(): Object /* flash.display.Sprite */
        {
            var loc1=null;
            if (!PetMingBagBaseItem._toUnlockMenu) 
            {
                PetMingBagBaseItem._toUnlockMenu = new Object /* flash.display.Sprite */();
                ObjCreate.createBg(0, 0, 70, 30, PetMingBagBaseItem._toUnlockMenu, ImagesConst.ToolTipBg);
                loc1 = ObjCreate.createLabel(Language.getString(54931), 23, 7, 30, 20, "left", PetMingBagBaseItem._toUnlockMenu, false, 58623);
                loc1.buttonMode = true;
                loc1.mouseChildren = false;
                loc1.mouseEnabled = true;
            }
            return PetMingBagBaseItem._toUnlockMenu;
        }

        /* internal */ /* var */_lockedIconBox: Object /* flash.display.Sprite */;

        public /* var */baseItem: PetMingBaseItem;

        /* internal */ /* var */_hasLockFlag: Boolean=false;

        /* internal */ static /* var */_toUnlockMenu: Object /* flash.display.Sprite */;
    }
